/**
 * @description 水波纹特效测试
 * @link 原项目地址 : https://github.com/ifengzp/cocos-awesome
 */

import EventComponent from "../../../../quick/components/EventComponent";


const {ccclass, property} = cc._decorator;

@ccclass
export default class ShaderWaterSpread extends EventComponent {


    private material : cc.Material = null!;
    private waveOffset : number = 0.0;
    private maxWaveOffset : number = 2.0;

    onLoad(): void {
        this.material = this.node.getComponent(cc.Sprite).getMaterial(0);
        this.material.setProperty("canvas_size",[this.node.width,this.node.height]);
        this.onN(this.node,cc.Node.EventType.TOUCH_END,this.onClick);
    }

    private onClick( ev  : cc.Event.EventTouch ){
        let pos = ev.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos);
        pos.x += this.node.anchorX * this.node.width;
        pos.y += this.node.anchorY * this.node.height;
        // UV 坐标是左上角(0,0)

        this.material.setProperty("center",[pos.x / this.node.width, (this.node.height - pos.y) / this.node.height]);
        this.waveOffset = 0.0;
    }

    protected update(dt: number): void {
        if (this.waveOffset > this.maxWaveOffset ) return;

        this.waveOffset += dt;
        this.material.setProperty("wave_offset",this.waveOffset);
    }
}
